#include <iostream>
#include <thread>
#include <string>
#include<functional>

class widget
{
private:
    std::string m_data;
    int m_id;

public:
    widget() = default;
    widget(std::string data, int id) : m_data(data), m_id(id) {}

    void toString()
    {
        std::cout<<"id: "<<m_id<<", data: "<<m_data<<std::endl;
    }

    friend void update_data(widget& w);
};

void update_data(widget& w)
{
    w.m_data = "new value";
    w.m_id = 111;
}

void f(int i, const std::string &s)
{
    std::cout << i << "---" << s << std::endl;
}

void oops(int some_param)
{
    char buff[1024];
    sprintf(buff, "%i", some_param);
    // 这里错误，不能将字符数组传给线程函数
    // thread 并不会隐式转换为string，需要手动转为string
    // std::thread t(f,some_param,buff);
    std::thread t(f, some_param, std::string(buff));
    t.join();
}

void oops_again(widget& w)
{
    // 这里要传递引用必须用std::ref
    // std:: thread t(update_data,w);
    std:: thread t(update_data,std::ref(w));
    t.join();
}

int main()
{
    int i = 333;
    oops(i);

    widget w("old value",0);
    w.toString();
    oops_again(w);
    std::cout<<"after"<<std::endl;
    w.toString();

    return 0;
}